<?php
/**
 * Created by PhpStorm.
 * User: hanbin
 * Date: 17/4/11
 * Time: 下午8:55
 */

namespace App\Logic;

use App\Server\MessageModel;

/**
 * 消息
 *
 * Class MessageLogic
 * @package App\Logic
 */
class MessageLogic extends BaseLogic
{
    /**
     * 消息大类
     * @param string $userId
     * @return array
     */
    public static function typeList($userId = '')
    {
        if ($userId) {
            $result = [];
            $detail = MessageModel::getList(10, null, 1, ['member_id' => $userId, 'type' => 1, 'status' => 1]);
            if ($detail) {
                $list = $detail['data'];
                if ($list) {
                    $v = $list[0];
                    $item = [];
                    $item['id'] = $v['id'];
                    $item['type'] = $v['type'];
                    $item['typeName'] = MessageModel::$TypeName[$v['type']];
                    $item['content'] = $v['content'];
                    $item['create_time'] = $v['create_time'];
                    $result[] = $item;
                }
            } else {
                $item = [];
                $item['id'] = 0;
                $item['type'] = 1;
                $item['typeName'] = MessageModel::$TypeName[1];
                $item['content'] = "无";
                $item['create_time'] = 0;
                $result[] = $item;
            }
            $detail = MessageModel::getList(10, null, 1, ['member_id' => $userId, 'type' => 2, 'status' => 1]);
            if ($detail) {
                $list = $detail['data'];
                if ($list) {
                    $v = $list[0];
                    $item = [];
                    $item['id'] = $v['id'];
                    $item['type'] = $v['type'];
                    $item['typeName'] = MessageModel::$TypeName[$v['type']];
                    $item['content'] = $v['content'];
                    $item['create_time'] = $v['create_time'];
                    $result[] = $item;
                } else {
                    $item = [];
                    $item['id'] = 0;
                    $item['type'] = 2;
                    $item['typeName'] = MessageModel::$TypeName[2];
                    $item['content'] = "无";
                    $item['create_time'] = 0;
                    $result[] = $item;
                }
            }
            $detail = MessageModel::getList(10, null, 1, ['member_id' => $userId, 'type' => 3, 'status' => 1]);
            if ($detail) {
                $list = $detail['data'];
                if ($list) {
                    $v = $list[0];
                    $item = [];
                    $item['id'] = $v['id'];
                    $item['type'] = $v['type'];
                    $item['typeName'] = MessageModel::$TypeName[$v['type']];
                    $item['content'] = $v['content'];
                    $item['create_time'] = $v['create_time'];
                    $result[] = $item;
                } else {
                    $item = [];
                    $item['id'] = 0;
                    $item['type'] = 3;
                    $item['typeName'] = MessageModel::$TypeName[3];
                    $item['content'] = "无";
                    $item['create_time'] = 0;
                    $result[] = $item;
                }
            }
            return self::success($result, 200, 'ok');
        } else {
            return self::error('请登录', 206);
        }
    }

    /**
     * 消息列表
     *
     * @param $userId
     * @param $params
     * @return array
     */
    public static function msgList($userId, $params)
    {
        if (!$userId) {
            return self::error('请登录', 206);
        }
        if (!$params) {
            return self::error('请求参数有误', 206);
        }
        $pagesize = isset($params['pagesize']) ? $params['pagesize'] : 10;
        $lastId = isset($params['last_id']) ? $params['last_id'] : 1;
        $type = isset($params['type']) ? $params['type'] : 1;
        $detail = MessageModel::getList($pagesize, null, $lastId, ['member_id' => $userId, 'type' => $type, 'status' => 1]);
        if ($detail) {
            $list = $detail['data'];
            $result = [];
            if ($list) {
                foreach ($list as $k => $v) {
                    $item = [];
                    $item['id'] = $v['id'];
                    $item['title'] = $v['title'];
                    $item['content'] = $v['content'];
                    $item['related_id'] = $v['related_id'];
                    $item['create_time'] = $v['create_time'];
                    $result[] = $item;
                }
                $detail['data'] = $result;
            }
        }
        return self::success($detail, 200, 'ok');
    }

}